<?php
session_start();
// Clear all previous messages.
if($_SESSION['log-err']) session_unregister('log-err');
if($_SESSION['log-info']) session_unregister('log-info');

$year= $_POST['year'];
$grade = $_POST['grade'];
$term= $_POST['term'];
$class = $_POST['class'];



include('./dbconnection.php');
// Check whether class is already registerd.
$query_check = "SELECT * FROM class WHERE year = '$year' AND grade='$grade' AND term='$term' AND class='$class' ";


if($class_exsist = mysql_query($query_check)){
	if(mysql_num_rows($class_exsist)>0){
		// class is already registered.
		$_SESSION['log-err'] = "Class Already Exists.";
	}else{
		//add class to the database
		$query_add = "INSERT INTO class ( year, grade, term, class )
                       VALUES  ('$year','$grade','$term' ,'$class')";
		if(mysql_query($query_add)){
			// class add successful.
			$_SESSION['log-info'] = "Add class Successfully.";
		}else{
			// SQL error occured.
			$_SESSION['log-err'] = "Sorry. Error occured. Please try again.";
		}
	}
}


else{
	// Incomplet form submission.
	$_SESSION['log-err'] = "All the fields should be filled.";

}
$_SESSION['page'] = "addClass.php";
header('Location: ../');


?>